package com.my.blog.website.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.my.blog.website.dao.QuestionVoMapper;
import com.my.blog.website.dto.Types;
import com.my.blog.website.modal.Vo.ContentVo;
import com.my.blog.website.modal.Vo.ContentVoExample;
import com.my.blog.website.modal.Vo.QuestionVo;
import com.my.blog.website.modal.Vo.QuestionVoExample;
import com.my.blog.website.service.IQuestionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description 问答服务实现
 * @ClassName QuestionServiceImpl
 * @Author gyh
 * @date 2020.08.03 15:06
 */
@Service
public class QuestionServiceImpl implements IQuestionService {

    @Resource
    private QuestionVoMapper questionVoMapper;


    /**
     * @param p
     * @param limit
     * @MethodName: getQuestions
     * @Description: 分页查询问答列表
     * @Param: [p, limit]
     * @Return: com.github.pagehelper.PageInfo<com.my.blog.website.modal.Vo.QuestionVo>
     * @Author: gyh
     * @Date: 2020-08-03 15:14
     */
    @Override
    public PageInfo<QuestionVo> getQuestions(int p, int limit) {
        List<QuestionVo> data = null;
        QuestionVoExample example = new QuestionVoExample();
        example.setOrderByClause("gmt_create desc");
        PageHelper.startPage(p, limit);
        data = questionVoMapper.selectByExampleWithBLOBs(example);
        PageInfo<QuestionVo> pageInfo = new PageInfo<>(data);
        return pageInfo;
    }

    /**
     * @param question
     * @Author: gyh
     * @Description: 问答保存
     * @DateTime: 2020/9/20 23:51
     * @Params: [question]
     * @Return void
     */
    @Override
    public void save(QuestionVo question) {
        questionVoMapper.insert(question);
    }

    /**
     * @param qid
     * @Author: gyh
     * @Description: 获取问题信息
     * @DateTime: 2020/9/21 23:55
     * @Params: [qid]
     * @Return com.my.blog.website.modal.Vo.QuestionVo
     */
    @Override
    public QuestionVo getQuestionByQid(String qid) {
        QuestionVoExample example = new QuestionVoExample();
        QuestionVoExample.Criteria criteria = example.createCriteria();
        criteria.andQidEqualTo(qid);
        return questionVoMapper.selectByExampleWithBLOBs(example).stream().findFirst().orElse(null);
    }

    @Override
    public void update(QuestionVo question) {
        questionVoMapper.updateByPrimaryKeySelective(question);
    }

}
